package servletPackage;

import inspur.dao.BookDao;
import inspur.dao.CookieDao;
import inspur.dao.UserDao;
import inspur.entity.BookObject;
import inspur.entity.CookieTokenObject;
import inspur.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UploadBookServlet")
public class DeleteUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();
        String postUserID=request.getParameter("userId");
        if (postUserID==null){
            out.print("提醒，需要填写一个id编号");
            return;
        }else{
            if (postUserID.equals("")){
                out.print("提醒，需要填写一个id编号");
                return;
            }
        }
        //
        String CookieUserid=CookieDao.getCookieItemValue(request,"userid");
        if (CookieUserid==null){
            out.print("删除失败，原因：你没有登录");
        }else{

            User SQLmyUser=UserDao.getUserByUserId(CookieUserid);
            User postUser=UserDao.getUserByUserId(postUserID);
            CookieTokenObject cto= CookieDao.CreatCookieTokenObject(request);
            int flag=0;
            boolean ctoIsOk=CookieDao.isTrueToken(cto,SQLmyUser.getPassword(),request);
            if (ctoIsOk){
                if (SQLmyUser.getIdentity().equals("admin")){
                        if (postUser.getIdentity().equals("admin")){
                            out.println("移除用户失败，原因：你没有权限执行该操作，对方是administrative用户组。你必须是root用户组才可以执行该操作");
                        }else  if (postUser.getIdentity().equals("root")){
                            out.println("移除用户失败，原因：你没有权限执行该操作，对方是root用户组。");
                        }else{
                            //移除
                            flag=UserDao.delectUser(postUserID);
                        }
                }else if (SQLmyUser.getIdentity().equals("root")){
                        //移除
                    flag=UserDao.delectUser(postUserID);
                }else {
                    out.println("移除用户失败，原因：你没有权限执行该操作，你必须是administrative用户组才可以执行该操作");
                }
            }else {
                out.print("移除失败：你的登录信息失效");
            }
            if (flag==2){
                out.print("success");
            }else if (flag==3){
                out.print("删除失败，原因：user编号重复或丢失");
            }else {
                out.print("数据库出现未知错误，请联系管理员");
            }

        }
        out.close();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
